package com.young.test.aop;

import javax.inject.Named;

import org.aspectj.lang.ProceedingJoinPoint;
import org.aspectj.lang.annotation.Aspect;


@Named
@Aspect
public class ConcurrentOperationExecutor {

	private static final int DEFAULT_MAX_RETRIES = 2;
	
	private int maxRetries = DEFAULT_MAX_RETRIES;

	public int getMaxRetries() {
		return maxRetries;
	}

	public void setMaxRetries(int maxRetries) {
		this.maxRetries = maxRetries;
	}
	
	public Object doConcurrentOperation(ProceedingJoinPoint pjp) throws Throwable {
		int numAttempts = 0;
		RuntimeException exception;
		do{
			numAttempts++;
			try{
				return pjp.proceed();
			}catch(RuntimeException ex){
				exception = ex;
			}
		}while(numAttempts <= this.maxRetries);
		throw exception;
	}
	
}
